package com.example.administrator.himalaya.activity;

import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.sqlHelper.Helper;

public class lishi_mostActivity extends AppCompatActivity implements View.OnClickListener {
    private SimpleCursorAdapter mAdapterm;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lishi_most);
        ImageView back = (ImageView) findViewById(R.id.backhaha);
        TextView clean = (TextView) findViewById(R.id.clean);
        back.setOnClickListener(this);
        clean.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.listView);
        Helper helper = new Helper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(
                Helper.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int index = cursor.getColumnIndex("title");
                int index1 = cursor.getColumnIndex("name");
                int index2 = cursor.getColumnIndex("number");
                if (index != -1) {
                    String title = cursor.getString(index);
                    Log.d("1111", "title: " + title);
                }
                if (index1 != -1) {
                    String name = cursor.getString(index1);
                    Log.d("1111", "time: " + name);
                }
                if (index2 != -1) {
                    String number = cursor.getString(index2);
                    Log.d("1111", "name: " + number);
                }
                mAdapterm = new SimpleCursorAdapter(
                        this,
                        R.layout.china,
                        cursor,
                        new String[]{"title", "name", "number"},
                        new int[]{R.id.china_title, R.id.china_ing, R.id.china_per},
                        SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
                );
                mListView.setAdapter(mAdapterm);
            }
        }
    }

    @Override
    public void onClick(View view) {
        Helper helper = new Helper(this);
        final SQLiteDatabase db = helper.getReadableDatabase();
        switch (view.getId()) {
            case R.id.backhaha:
                finish();
                break;
            case R.id.clean:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("温馨提示")
                        .setMessage("是否清空收听历史")
                        .setIcon(R.mipmap.icon_radio_country);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        db.delete(Helper.TABLE_NAME,
                                null,
                                null
                        );
                        flash(db);
                    }
                });
                builder.show();
                break;
        }
    }

    private void flash(SQLiteDatabase db) {
        Cursor cursor = db.query(Helper.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );
        mAdapterm = new SimpleCursorAdapter(
                this,
                R.layout.china,
                cursor,
                new String[]{"title", "name", "number"},
                new int[]{R.id.china_title, R.id.china_ing, R.id.china_per},
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
        );
        mListView.setAdapter(mAdapterm);
    }
}
